package com.lyf.boot.modules.index.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.lyf.boot.base.system.entity.SysRole;
import com.lyf.boot.exception.ServiceException;
import com.lyf.boot.modules.index.entity.ProfileVo;
import com.lyf.boot.modules.index.service.IProfileService;
import com.lyf.boot.modules.system.dao.SysUserDao;
import com.lyf.boot.utils.AuthUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 个人信息服务
 * @author Luyufan
 * @date 2023/7/6 16:19
 */
@Service
@RequiredArgsConstructor
public class ProfileServiceImpl implements IProfileService {

    private final SysUserDao sysUserDao;

    @Override
    public ProfileVo getProfile() {
        Long userId = AuthUtil.getUserId();
        ProfileVo profileVo = sysUserDao.selectUserProfile(userId);
        if(profileVo == null){
            throw new ServiceException("账号不存在");
        }
        List<SysRole> userRoles = sysUserDao.selectUserRole(userId);
        if(CollUtil.isNotEmpty(userRoles)){
            profileVo.setRoles(BeanUtil.copyToList(userRoles, ProfileVo.ProfileRole.class));
        }
        return profileVo;
    }

    @Override
    public List<String> getUserRoles(Long userId) {
        return sysUserDao.selectUserRoleCode(userId);
    }

    @Override
    public List<String> getUserPermissions(Long userId) {
        return sysUserDao.selectUserPermission(userId);
    }
}
